import { AccessEnum } from '@/utils/accessUtil'
import 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import BasicLayout from '@/layouts/BasicLayout.vue'

// 扩展Vue Router的RouteMeta接口，确保meta.title是必填项
declare module 'vue-router' {
  interface RouteMeta {
    title: string
    // 是否需要登录
    isLogin: boolean
    // 路由权限
    access?: string
    // 是否显示在菜单中
    hidden?: boolean
  }
}

export const routes: RouteRecordRaw[] = [
  // 登录页路由 (保持顶层，独立)
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录', isLogin: false, hidden: true }
  },
  // 注册页路由
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/Register.vue'),
    meta: { title: '注册', isLogin: false, hidden: true }
  },
  // 其他需要布局的页面，作为 BasicLayout 的子路由
  {
    path: '/',
    name: 'layout',
    component: BasicLayout,
    children: [
      {
        path: '',
        name: 'home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页', isLogin: false }
      },
      {
        path: '/problem',
        name: 'problemBank',
        component: () => import('@/views/problem/index.vue'),
        meta: { 
          title: '题库', 
          isLogin: false
        }
      },
      {
        path: '/admin/problem',
        name: 'adminProblem',
        component: () => import('@/views/admin/problem/index.vue'),
        meta: { 
          title: '题目管理', 
          isLogin: true, 
          access: AccessEnum.ADMIN
        }
      },
      {
        path: '/admin/problem/add',
        name: 'adminProblemAdd',
        component: () => import('@/views/admin/problem/ProblemEdit.vue'),
        meta: { 
          title: '创建题目', 
          isLogin: true, 
          access: AccessEnum.ADMIN,
          hidden: true
        }
      },
      {
        path: '/admin/problem/edit/:id',
        name: 'adminProblemEdit',
        component: () => import('@/views/admin/problem/ProblemEdit.vue'),
        meta: { 
          title: '编辑题目', 
          isLogin: true, 
          access: AccessEnum.ADMIN,
          hidden: true
        }
      },
     
      {
        path: '/problem/:id',
        name: 'problemDetail',
        component: () => import('@/views/problem/Detail.vue'),
        meta: { 
          title: '题目详情', 
          isLogin: false,
          hidden: true
        }
      },
      {
        path: '/problem/solution/publish',
        name: 'solutionPublish',
        component: () => import('@/views/problem/SolutionPublish.vue'),
        meta: { 
          title: '发布题解', 
          isLogin: true,
          hidden: true
        }
      },
      {
        path: '/user/info',
        name: 'userInfo',
        component: () => import('@/views/user/UserInfo.vue'),
        meta: { 
          title: '个人资料', 
          isLogin: false,
          hidden: true
        }
      },
      {
        path: '/submission-detail/:submissionId',
        name: 'submissionDetail',
        component: () => import('@/views/submission/SubmissionDetail.vue'),
        meta: {
          title: '提交详情',
          isLogin: true,
          hidden: true
        }
      },
      {
        path: '/solution/:solutionId',
        name: 'solutionDetail',
        component: () => import('@/views/problem/SolutionDetail.vue'),
        meta: {
          title: '题解详情',
          isLogin: false, // 题解详情通常不需要登录即可查看
          hidden: true
        }
      }
    ]
  }
]
